# coding=utf-8
"""
Author: Mr.夏末
@Function: 关键字驱动的运行
"""
import os
import re
import time

from class11.webkeys import Web
from selenium.webdriver import ActionChains

web = Web()
web.openbrowser()
driver = web.driver

#打开12306登录页
web.get_url('https://kyfw.12306.cn/otn/resources/login.html')
web.input('//input[@id="J-userName"]','17863087593')
web.input('//input[@id="J-password"]','shuai21654')
web.click("J-login")
web.slide12306()
web.sleep(1)

# 打开电商网站登录页
web.get_url("http://47.105.110.138:8000/index.php/Home/user/login.html")
web.input('username','2529085125@qq.com')
web.input('//*[@id="password"]','123456')
ver = web.get_verify()
# 输入验证码
web.input('//*[@id="verify_code"]',ver)
# 点击登录
web.click('//a[contains(text(),"登")]')

# 获取昵称
nickname = web.get_text('//a[@class="red userinfo"]')
print(nickname)

#搜索
web.input('//*[@id="q"]','手机')
web.click('//*[@id="sourch_form"]/a')

# 点击第1个加入购物车
web.click('//a[contains(text(),"小米手机")]/../..//a[text()="加入购物车"]')
web.click('join_cart')
# 强制等待
web.sleep(1)
web.click('//*[@id="layui-layer1"]/span/a')

# 结算
web.click('//*[@id="hd-my-cart"]/a/div/span')
web.click("//a[text()='去结算']")
web.click("//button[contains(text(),'提交订单')]")

# 获取订单号
print(web.get_text('//p[@class="succ-p"]'))
orders = web.get_text('//p[@class="succ-p"]')
orderid = re.findall(r'\d{18}',orders)
print(orderid)
web.sleep(1)
# 点击我的订单
web.click("//a[text()='我的订单']")

# 切换窗口
# 获取窗口的标志id列表
handles = driver.window_handles
print(handles)
# 下标切换
# driver.switch_to.window(handles[1])

# 标题切换
for i in handles:
    driver.switch_to.window(i)
    if driver.title.__contains__('我的订单'):
        break

# 搜索订单号
web.input('//*[@id="search_key"]',orderid)
web.click('//*[@id="search_order"]/input[2]')

# 悬停
ele = driver.find_element('xpath','//span[text()="账户设置"]')
# 创建对象
action = ActionChains(driver)
# 鼠标移动到元素上
action.move_to_element(ele).perform()

# 修改个人信息
web.click("(//a[text()='个人信息'])[2]")
# driver.find_element('link text','个人信息').click()
# 图片上传
web.click('//*[@id="preview"]')
# 进入iframe
iframe = driver.find_element('xpath','//iframe')
driver.switch_to.frame(iframe)
# 获取文件路径
path = os.path.join(os.path.dirname(os.path.dirname(__file__)),'lib','mm.jpg')
print(path)
# driver.find_element('xpath','//div[@id="filePicker"]/div[2]/input').send_keys(path)
# 如果有多个元素，默认取第一个元素
# ele = driver.find_element("xpath",'//input[@type="file"]')
# ele.send_keys(path)

# 如果有多个元素，怎么获取指定个数
# 方式1：(xpath)[2]    下标从1开始
# ele = driver.find_element("xpath",'(//input[@type="file"])[2]')
# ele.send_keys(path)

# 方式2：获取到所有元素，从元素列表里面取第几个
ele = driver.find_elements("xpath",'//input[@type="file"]')
ele[1].send_keys(path)
web.sleep(1)
# 显示等待
# WebDriverWait(driver,10,1).until(EC.text_to_be_present_in_element((By.XPATH,"//div[@class='info']"),'已上传1份'))

# 点击确定使用
web.click('//div[@class="saveBtn"]')
driver.switch_to.default_content()
web.click('//input[@class="save"]')

# 地址管理
web.click("//a[text()='地址管理']")
# 删除地址
web.clicktry("(//span[text()='哼哼哈嘿']/ancestor::ul//a[text()='删除'])[1]")
web.sleep(1)

count = web.get_text('//em[@class="red"]')

# 新增地址red
web.click('//span[text()="增加新地址"]')
web.input('//input[@name="consignee"]','哼哼哈嘿')
web.input('//input[@name="mobile"]','18854725230')
# 找到select元素
web.select('//select[@id="province"]','28240')
web.select('//select[@id="city"]','28241')

# 文本选择
web.select('//select[@id="district"]','黄埔区')
web.select('//select[@id="twon"]','红山街道')

web.input("//input[@name='address']","奔流到海不复回")
web.input("//input[@name='zipcode']",'272405')

web.click('//a[@id="address_submit"]')
web.sleep(1)

count1 = web.get_text('//em[@class="red"]')
print(count1,count)
web.assertno(count1,count)